package io.zhengsh.vvip.leetcode.twopointer;

import java.util.HashMap;
import java.util.Map;

public class Q167_Two_Sum_II_Input_Array_Is_Sorted {
    class Solution {
        public int[] twoSum(int[] numbers, int target) {
            int low = 0, high = numbers.length;
            while (low < high) {
                int sum = numbers[low] + numbers[high];
                if (sum == target) {
                    return new int[]{low + 1, high + 1};
                } else if (sum < target) {
                    ++low;
                } else {
                    --high;
                }
            }
            return new int[]{-1, -1};
        }

        public int[] twoSum1(int[] numbers, int target) {
            if (numbers == null || numbers.length == 0) {
                return new int[2];
            }
            Map<Integer, Integer> map = new HashMap<>();
            map.put(numbers[0], 0);
            for (int i = 1; i < numbers.length; i++) {
                if (map.get(target - numbers[i]) != null) {
                    return new int[]{map.get(target - numbers[i]) + 1, i + 1};
                } else {
                    map.put(numbers[i], i);
                }
                if (numbers[i] > target) {
                    break;
                }
            }
            return new int[2];
        }
    }
}
